package kku.cs.fgl;

import org.newdawn.slick.Input;

public class DragController implements ActorController {
	static private Actor dragObject = null;

	private int drag = 0;

	private int mx, my;

	private int ax, ay;

	public void collideIn(Actor actor1, Actor actor2) {

	}

	public void collideOut(Actor actor1, Actor actor2) {

	}

	public void mouseClicked(Actor actor, int button) {

	}

	public void mouseEntered(Actor actor) {
	}

	public void mouseExited(Actor actor) {

	}

	public void update(Actor actor, int time) {
		if (actor.isMouseEnterState()) {
			if (drag == 0 && !actor.input.isMouseButtonDown(0)) {
				drag = 1;
			}
			if (drag == 1 && actor.input.isMouseButtonDown(0)
					&& dragObject == null) {
				drag = 2;
				mx = actor.input.getMouseX();
				my = actor.input.getMouseY();
				ax = (int) actor.getX();
				ay = (int) actor.getY();
				dragObject = actor;
			}
		} else if (drag == 1) {
			drag = 0;
			if (dragObject == actor)
				dragObject = null;
		}
		if (drag == 2) {
			int nx = actor.input.getMouseX() - mx;
			int ny = actor.input.getMouseY() - my;
			actor.setLocation(ax + nx, ay + ny);

			if (!actor.input.isMouseButtonDown(0)) {
				drag = 0;
				if (!drop(actor)) {
					actor.setLocation(ax, ay);
				}
				if (dragObject == actor)
					dragObject = null;
			}
		}
		if (actor.input.isKeyDown(Input.KEY_ESCAPE)) {
			if (drag == 2) {
				actor.setLocation(ax, ay);
			}
			drag = 0;
			dragObject = null;

		}
	}

	public boolean drop(Actor actor) {
		return false;
	}
}
